package com.lenovo.tool;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TranslationPrint {
    private static final String TAG_STRING = "string";
    private static final String TAG_STRING_ARRAY = "string-array";
    private static final String TAG_PLURALS = "plurals";

    private static void findMissingTranslations(File baseDir, XmlResFileFilter filter,int len) throws IOException {   
        //int len = baseDir.getAbsolutePath().length();
        File[] files = baseDir.listFiles();
        for (File base : files) {
        	String path = base.getAbsolutePath().substring(len + 1);
			//System.out.println("relative "+path);
			if (filter.shallExclude(path)){
				continue;
			}
        	if (base.isDirectory()){
        		findMissingTranslations(base,filter,len);
        		continue;
        	}
        	String mAbsolutePath = base.getAbsolutePath().toLowerCase();
        	if (!mAbsolutePath.endsWith(".xml")){
        		continue;
        	} 
        	if (!base.getParentFile().getName().equals("values")){
        		continue;
        	}        	
        	
        	if(isContinue(base.getName()))
        	{
        		continue;
        	}
			try {
				Document baseDoc;
				try {
					baseDoc = DocumentUtils.openDocument(base);
					Element root = baseDoc.getDocumentElement();		            
		            NodeList nodeList = root.getElementsByTagName(TAG_STRING);
		            if (null != nodeList && nodeList.getLength()>0){
		            	System.out.println(base.getAbsolutePath()); 
		            	continue;
		            }
		            nodeList = root.getElementsByTagName(TAG_STRING_ARRAY);
		            if (null != nodeList && nodeList.getLength()>0){
		            	System.out.println(base.getAbsolutePath()); 
		            	continue;
		            }
		            nodeList = root.getElementsByTagName(TAG_PLURALS);
		            if (null != nodeList && nodeList.getLength()>0){
		            	System.out.println(base.getAbsolutePath()); 
		            	continue;
		            }
				} catch (SAXException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}	            
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			}
            
        }    
    }

    private static boolean isContinue(String name) throws IOException
	{
    	String[] fileNames = FileFilter.getInstance().getAllFiles();
    	for (String fileName : fileNames)
		{
			if(name.equals(fileName))
			{
				return true;
			}
		}
		return false;
	}

	public static void main(String[] args) {
    	if (args.length != 1) {
            System.out.println("please enter the config file name");
            return;
        }

    	//File configFile = new File("D:\\Users\\lenovo6\\workspace\\TranslationTool\\missing_translation.xml");

        File configFile = new File(args[0]);
        if (!configFile.exists() || !configFile.isFile()) {
            System.out.println("config file error");
            return;
        }

        try {
            PropConfigHandler handler = new PropConfigHandler();

            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            parser.parse(configFile, handler);

            Properties props = handler.getProps();
            ArrayList<String> excludePathList = handler.getExcludePaths();
  //          HashSet<String> excludeArrays = handler.getExcludeArrays();

            File baseDir = new File(props.getProperty("basedir"));	
  //          String baseLocale = props.getProperty("baselocale");
            
            if (!baseDir.exists()) {
                System.out.println("config file error");
            }
			int len = baseDir.getAbsolutePath().length();
			XmlResFileFilter filter = new XmlResFileFilter(true);
        	for (String path : excludePathList) {
				//System.out.println("excludePathList "+path);
            	filter.addExludePath(path);
        	}
            findMissingTranslations(baseDir,filter,len);
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
